#ifndef __BHTTP_BTHREAD_POOL_H__
#define __BHTTP_BTHREAD_POOL_H__
#pragma once

#include <queue>
#include <mutex>
#include <condition_variable>
#include "bthread.h"

namespace bhttp
{

class bthreadpool
{
	using btask = std::function<void()>;
public:
	bthreadpool(const bthreadpool&) = delete;
	bthreadpool& operator=(const bthreadpool&) = delete;

	explicit bthreadpool(const std::string& name = std::string("bthreadpool"));
	~bthreadpool();

	void start(int num_thd);
	void stop();
	void run(const btask& tk);
	void run(btask&& tk);

private:
	void thread_func();
	bool is_full() const;
	btask take();

private:
	std::string name_;
	bool running_{ false };
	std::vector<std::shared_ptr<bthread>> threads_;
	std::deque<btask> queue_;
	std::mutex mutex_;
	std::condition_variable not_full_;
	std::condition_variable not_empty_;
	size_t max_queue_size_{ 0 };
};

}

#endif //__BHTTP_THREAD_POOL_H__
